/*
 * Created by SharpDevelop.
 * User: tentus
 * Date: 2006-10-17
 * Time: 18:02
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using System.Xml.XPath;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;

namespace ActionManager
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	/// 	
			
	public partial class MainForm 
	{								
		private string xmlFilePath = Path.GetDirectoryName(Application.ExecutablePath)+"\\ActionManager.xml";
		private ActionManagerXml xmlManager;
		private string xml;
		private ShortcutServer shortcutServer;
		private ScheduleServer scheduleServer;
		private ActionList listaAkcji;
		
		[STAThread]
		public static void Main(string[] args)
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new MainForm());			
		}		
		
		public MainForm()
		{			
            InitializeComponent();
            			
            xmlManager = new ActionManagerXml();
            xml = xmlManager.LoadXml(xmlFilePath);
            
            if (xml=="")
            	xml = xmlManager.CreateEmptyGroupXml();
            
            listaAkcji     = new ActionList();
            
            shortcutServer = new ShortcutServer();
            scheduleServer = new ScheduleServer();            
			LoadGroupListForm();
			NotifyServers();						
		}					
		
		private void NotifyShortcut(string _xml)
		{
			shortcutServer.Notify(_xml);
		}
		
		private void NotifySchedule(string _xml)
		{
			scheduleServer.Notify(_xml);
		}
		
		private void NotifyServers()
		{			
			NotifyShortcut(xml);
			NotifySchedule(xml);
		}				
		
		private void LoadGroupListForm()
		{
			int indx = listBox1.SelectedIndex;
				
			listBox1.Items.Clear();									
			
			List<string> list = new List<string>();
			xmlManager.GetGroupList(xml, list);
			
			for (int i=0; i<list.Count; ++i)
				listBox1.Items.Add(list[i]);			
			
			if (indx<listBox1.Items.Count)
			  listBox1.SelectedIndex = indx;
			
		}
		
		private void AddGroupForm()
		{
			string xml_group = listaAkcji.ShowForm("");
			if (xml_group=="")return;
			
			xml = xmlManager.AddGroup(xml, xml_group);											
			
			xmlManager.SaveXml(xml, xmlFilePath);//zapisywanie ustawien prz kazdej poprawnej operacji
		}
		
		private void EditGroupForm()
		{
			if (listBox1.Items.Count<1 || listBox1.SelectedIndex<0)return;
			
			string xml_group = xmlManager.GetGroup(xml, listBox1.SelectedIndex);
			xml_group = listaAkcji.ShowForm(xml_group);
			
			xml = xmlManager.EditGroup(xml, xml_group, listBox1.SelectedIndex);						
			
			xmlManager.SaveXml(xml, xmlFilePath);//zapisywanie ustawien prz kazdej poprawnej operacj
		}
		
		private void DeleteGroupForm()
		{
			if (listBox1.Items.Count<1 || listBox1.SelectedIndex<0)return;
			
			xml = xmlManager.DeleteGroup(xml, listBox1.SelectedIndex);
			
			xmlManager.SaveXml(xml, xmlFilePath);//zapisywanie ustawien prz kazdej poprawnej operacj
		}
				
		
		void Button2Click(object sender, System.EventArgs e)
		{			
			EditGroupForm();
			
			LoadGroupListForm();
			
			NotifyServers();
		}
				
		
		void Button1Click(object sender, System.EventArgs e)
		{
			
			AddGroupForm();
			
			LoadGroupListForm();
			
			NotifyServers();
		}
		
		void Button4Click(object sender, System.EventArgs e)
		{
			Close();
		}
		
		void Button3Click(object sender, System.EventArgs e)
		{						
			DeleteGroupForm();
			
			LoadGroupListForm();
			
			NotifyServers();
		}													
		
		void Button5Click(object sender, System.EventArgs e)
		{			
			//testowanie
					
			string tekst = Path.GetDirectoryName(Application.ExecutablePath);
			
			//foreach (Process ble in Process.GetProcesses())
			//{
			//	tekst += ble.ProcessName+"\r\n";
			//}
			
			XmlFormShow xmlForm = new XmlFormShow();
			xmlForm.ShowForm(tekst);
		}
		
		void MainFormFormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
		{
			xmlManager.SaveXml(xml, xmlFilePath);
		}
		
		void ListBox1MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			EditGroupForm();
			
			LoadGroupListForm();
			
			NotifyServers();
		}				
		
		void MainFormResize(object sender, System.EventArgs e)
		{
			if (WindowState==FormWindowState.Minimized)
				Visible = false;				
		}
		
		void NotifyIcon1DoubleClick(object sender, System.EventArgs e)
		{
			if (WindowState==FormWindowState.Minimized)
			{
				Visible = true;
				WindowState = FormWindowState.Normal;
			}
		}
				
		
		void ContextMenuStrip1Opening(object sender, System.ComponentModel.CancelEventArgs e)
		{
			if (WindowState==FormWindowState.Minimized)
			{
				toolStripMenuItem3.Visible = false;
				toolStripMenuItem1.Visible = true;
			}
			else
			{							
				toolStripMenuItem3.Visible = true;
				toolStripMenuItem1.Visible = false;
			}
		}
		
		void ToolStripMenuItem2Click(object sender, System.EventArgs e)
		{
			Close();
		}
		
		void ToolStripMenuItem3Click(object sender, System.EventArgs e)
		{
			WindowState = FormWindowState.Minimized;
			Visible = false;
		}
		
		void ToolStripMenuItem1Click(object sender, System.EventArgs e)
		{
			Visible = true;
			WindowState = FormWindowState.Normal;
		}
	}			
}
